import { relations, sql } from 'drizzle-orm'
import { mysqlTable, timestamp, int, varchar, tinyint, text, longtext, bigint, index, uniqueIndex } from 'drizzle-orm/mysql-core'
import { createId ,getConstants,init } from '@paralleldrive/cuid2'

init({
     length: 64
}) 

export const user = mysqlTable(
    'user',
    { 
        id: varchar("id", { length: 64 }).default("0").$defaultFn(() => { return createId() }).primaryKey(),
        username: varchar('username', { length: 255 }).notNull().unique(),
        password: varchar('password', { length: 255 }).notNull(),
        email: varchar('email', { length: 255 }).notNull().unique(),
        salt: varchar('salt', { length: 64 }).notNull(),
        createdAt: timestamp('created_at').defaultNow(),
    }
)

export const blog = mysqlTable(
    'blog',
    {
        id: varchar("id", { length: 64 }).default("0").$defaultFn(() => { return createId() }).primaryKey(),
        userId: varchar("user_id", { length: 64 }).default("0"),
        content: longtext('content', {}),
        createdAt: timestamp('created_at').defaultNow(),
    },
    (t) => (
        [
            index('idx_user_id').on(t.userId),
        ]
    )
)

export const table = {
    user,
    blog
} as const

export type Table = typeof table